#!/usr/bin/env python

"""User functionality such as login, select language, etc.
"""

__author__ = 'Yong Liu'
__version__ = '0.1'
__date__ = '2010-07-29'

import imaplib

import web

from app.models import smtp
#from app.models import imap
from app.controllers.i18n import switch_language
from config import host, protocol, port, slogan

class login:
    def GET(self):
        pages = switch_language()
        return pages.login(slogan)
    
    def POST(self):
        i = web.input()

        #switch language on the fly and rerender the templates with the specific language
        pages = switch_language()

        #User login
        try:
            #Create a connection to the server on "host:port"
            #m = imap.imap4(host, protocol, port)
            m = web.ctx.server

            if m.login(i.account, i.password):
                web.ctx.session.account = i.account
                raise web.seeother('/index')
        except imaplib.IMAP4.error, e:
            return pages.login(slogan, 'failure')
        except imaplib.IMAP4.abort, e:
            return pages.login(slogan, 'failure')
        except imaplib.IMAP4.readonly, e:
            return pages.login(slogan, 'failure')
        
class select_lang:
    """Select language, store in cookie
    """
    def GET(self):
        i = web.input()
        if i.has_key('mylang') :
            language = [i.mylang]
            web.setcookie('mylang', i.mylang, 36000)
        raise web.seeother('/')
